package com.github.niefy.modules.wx.controller;

import com.github.niefy.common.utils.R;
import com.github.niefy.modules.sys.service.SysLogService;
import com.github.niefy.modules.wx.form.TemplateMsgBatchForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.niefy.modules.wx.service.TemplateMsgService;

/**
 * 微信网页授权相关
 */
@RestController
@RequestMapping("/TemplateMsg")
@Api(tags = {"微信公众号模板发送"})
@RequiredArgsConstructor
public class WxTemplateMsgController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    SysLogService sysLogService;

    private final WxMpService wxMpService;

    @Autowired
    private TemplateMsgService TemplateMsgService;


    /**
     * 批量向用户发送模板消息
     * 通过用户筛选条件（一般使用标签筛选），将消息发送给数据库中所有符合筛选条件的用户
     */
    @PostMapping("/sendMsg")
    @ApiOperation(value = "批量向用户发送模板消息", notes = "将消息发送给数据库中所有符合筛选条件的用户")
    public R sendMsgBatch(@RequestBody TemplateMsgBatchForm form) {
        this.wxMpService.switchoverTo(form.getAppid());
        TemplateMsgService.sendMxMsg(form);
        return R.ok();
    }


}
